package com.kizuki.jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import lombok.extern.slf4j.Slf4j;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 需要导入jar包
 * <dependency>
 *     <groupId>com.alibaba</groupId>
 *     <artifactId>druid</artifactId>
 *     <version>1.2.10</version>
 * </dependency>
 */
@Slf4j
public class DruidDemo {

    private static DruidDataSource dataSource;

    static {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        druidDataSource.setInitialSize(5);
        druidDataSource.setMaxActive(20);
        druidDataSource.setMinIdle(5);
        dataSource = druidDataSource;
    }

    public static void main(String[] args) {
        try {
            DruidPooledConnection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("select * from note");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                log.info("content:{}", resultSet.getString("content"));
            }
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            log.error("Sql执行异常, e:", e);
        }
    }

}
